package com.lp.guoguo.controller;

import com.lp.guoguo.service.QuartzJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务控制器
 */
@RestController
@RequestMapping("/api/jobs")
@Tag(name = "定时任务管理", description = "定时任务的CRUD操作")
public class JobController {

    private static final Logger log = LoggerFactory.getLogger(JobController.class);

    @Autowired
    private QuartzJobService quartzJobService;

    /**
     * 简单状态检查接口
     */
    @GetMapping("/status")
    public Map<String, Object> getStatus() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "系统正常运行中");
        result.put("time", System.currentTimeMillis());
        return result;
    }

    @PostMapping("/add")
    @Operation(summary = "添加定时任务")
    public ResponseEntity<String> addJob(@RequestParam("jobClassName") String jobClassName,
                                        @RequestParam("jobName") String jobName,
                                        @RequestParam("jobGroupName") String jobGroupName,
                                        @RequestParam("cronExpression") String cronExpression) {
        try {
            // 可以在这里添加任务参数
            Map<String, Object> jobData = new HashMap<>();
            
            quartzJobService.addJob(jobClassName, jobName, jobGroupName, cronExpression, jobData);
            return ResponseEntity.ok("添加定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("添加定时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/pause")
    @Operation(summary = "暂停定时任务")
    public ResponseEntity<String> pauseJob(@RequestParam("jobName") String jobName,
                                          @RequestParam("jobGroupName") String jobGroupName) {
        try {
            quartzJobService.pauseJob(jobName, jobGroupName);
            return ResponseEntity.ok("暂停定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("暂停定时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/resume")
    @Operation(summary = "恢复定时任务")
    public ResponseEntity<String> resumeJob(@RequestParam("jobName") String jobName,
                                           @RequestParam("jobGroupName") String jobGroupName) {
        try {
            quartzJobService.resumeJob(jobName, jobGroupName);
            return ResponseEntity.ok("恢复定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("恢复定时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    @Operation(summary = "更新定时任务")
    public ResponseEntity<String> updateJob(@RequestParam("jobName") String jobName,
                                          @RequestParam("jobGroupName") String jobGroupName,
                                          @RequestParam("cronExpression") String cronExpression) {
        try {
            // 先检查任务是否存在
            if (!quartzJobService.checkJobExists(jobName, jobGroupName)) {
                return ResponseEntity.badRequest().body("任务不存在: " + jobName);
            }
            
            quartzJobService.updateJobCron(jobName, jobGroupName, cronExpression);
            return ResponseEntity.ok("更新定时任务成功");
        } catch (SchedulerException e) {
            log.error("更新定时任务失败", e);
            return ResponseEntity.badRequest().body("更新定时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @Operation(summary = "删除定时任务")
    public ResponseEntity<String> deleteJob(@RequestParam("jobName") String jobName,
                                           @RequestParam("jobGroupName") String jobGroupName) {
        try {
            quartzJobService.deleteJob(jobName, jobGroupName);
            return ResponseEntity.ok("删除定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("删除定时任务失败: " + e.getMessage());
        }
    }

    @PostMapping("/run")
    @Operation(summary = "立即执行定时任务")
    public ResponseEntity<String> runJob(@RequestParam("jobName") String jobName,
                                        @RequestParam("jobGroupName") String jobGroupName) {
        try {
            quartzJobService.runJobNow(jobName, jobGroupName);
            return ResponseEntity.ok("立即执行定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("立即执行定时任务失败: " + e.getMessage());
        }
    }
} 